<?php


namespace app\api\controller\v1;


use wheat\Wheat;

/**
 * 2019-07-02
 * 去除接口返回的麦位信息
 * Class Radio
 * @package app\api\controller\v1
 */
class Radio extends \app\api\controller\Radio
{




    /**
     * 上麦、换麦、抱麦
     * */
    public function upWheat(){

        $post = request()->only(['wheat_id','type','user_id'],'post');
        //验证数据
        $result = $this->validate($post,'Wheat.up');
        if(true !== $result){
            api_return(0,$result);
        }
        //不传默认登录用户ID
        if(!empty($post['user_id'])){
            $post['user_id'] = dehashid($post['user_id']);
            if (!is_numeric($post['user_id'])) api_return(0,'参数错误');
        }

        $wheat = new Wheat();
        if(isset($post['type']) && $post['type']  == 1){//抱麦

            $this->checkPower($this->user_id);

            if ($post['wheat_id'] == 1){
                api_return(0,'主播位不能抱麦');
            }

            $ret = $wheat->embrace($post['user_id'],self::$room_id,$post['wheat_id'],getPowerCode($post['user_id'],self::$room_id));
        }else{//上麦

            $powerCode = $this->getPowerCode();
            if (self::$is_free){
                if (in_array($post['wheat_id'],self::$generalNotUp)){
                    if (!$powerCode){
                        api_return(0,'非管理员不能上麦');
                    }
                }
            }else{
                if ($post['wheat_id'] != 1){
                    api_return(0,'上麦需申请');
                }
            }

            if ($post['wheat_id'] == 1){
                $this->checkPower($this->user_id,'权限不足,不能上主播位');
            }
            $ret = $wheat->on($this->user_id,self::$room_id,$post['wheat_id'],getPowerCode($this->user_id,self::$room_id));
        }
        if($ret['code']){
            $this->refreshWheat(self::$room_id);

            api_return(1,$ret['msg']);

        }else{
            api_return(0,$ret['msg']);
        }
    }


}